package com.venue.hjt.venueclass.model;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.venue.hjt.venueclass.BaseApplication;
import com.venue.hjt.venueclass.bean.API;
import com.venue.hjt.venueclass.bean.LoginBean;
import com.venue.hjt.venueclass.bean.VenueBean;
import com.venue.hjt.venueclass.db.AddData;
import com.venue.hjt.venueclass.db.CardDao;
import com.venue.hjt.venueclass.db.VenueEvaluate;
import com.venue.hjt.venueclass.db.VenueGroup;
import com.venue.hjt.venueclass.db.VenueHour;
import com.venue.hjt.venueclass.db.VenueInfo;
import com.venue.hjt.venueclass.db.VenueKeyword;
import com.venue.hjt.venueclass.service.LocalService;
import com.venue.hjt.venueclass.service.RemoteService;
import com.venue.hjt.venueclass.utlis.ClassCalendar;
import com.venue.hjt.venueclass.utlis.Constant;
import com.venue.hjt.venueclass.utlis.OkhttPost;
import com.venue.hjt.venueclass.utlis.PrefUtild;

import java.util.List;

import okhttp3.Call;

/**
 * Created by xqx on 2018/1/9.
 *
 */
public class FActivityModel {
    private Context context;
    private Intent intents=new Intent();
    private String className="";
    public FActivityModel(Context context) {
        this.context = context;
    }
    //判断当前是否登录
    public  boolean  getIsnamepwd(){
        String username= PrefUtild.INSTANCE.getString(context,"class_no","");
        assert username != null;
        if (username.isEmpty()){
            return true;
        }
        return false;
    }
    //班级名称
    public String className(){
        List<VenueInfo> cardTeachers = CardDao.queryVenueInfo();
        if (cardTeachers.size()!=0) {
            return cardTeachers.get(0).getVenue_name();
        }
        return "暂无";
    }

//    public VenueHour getTime(){
//        BaseApplication.getDaoInstant().clear();
//        List<VenueHour> venueHours = CardDao.queryHourS("1", ClassCalendar.getWeekDays()+"");
//        String strhour = ClassCalendar.getHour();
//        double hour1 = Double.parseDouble(strhour);
//        for(VenueHour hour:venueHours){
////            Log.e("ATG",hour.getStart_time()+"    "+hour.getEnd_time());
//            String start_time = hour.getStart_time();
//            String start_time1 = start_time.substring(0, 2);
//            String start_time2 = start_time.substring(3,4);
//            String start_time3 = start_time.substring(4);
////            if (start_time3.equals("0")){
////                start_time3="1";
////            }
//            String end_time = hour.getEnd_time();
//            String end_time1 = end_time.substring(0, 2);
//            String end_time2 = end_time.substring(3,4);
//            String end_time3 = end_time.substring(4);
////            if (end_time3.equals("0")){
////                end_time3="1";
////            }
//            String s = start_time1 + "." + start_time2+start_time3;
//            String s1 = end_time1 + "." + end_time2+end_time3;
//            double v = Double.parseDouble(s);
//            double v1 = Double.parseDouble(s1);
//            if (v<hour1&&hour1<=v1){
////                Log.e("ATG",hour.getStart_time()+" shijian"+"  "+hour1+" "+v+"  "+v1+"  "+hour.getEnd_time());
//                return hour;
//            }
//        }
//        for(VenueHour hour:venueHours){
//            String start_time = hour.getStart_time();
//            String start_time1 = start_time.substring(0, 2);
//            String start_time2 = start_time.substring(3,4);
//            String start_time3 = start_time.substring(4);
////            if (start_time3.equals("0")){
////                start_time3="1";
////            }
//            String s = start_time1 + "." + start_time2+start_time3;
//            double v = Double.parseDouble(s);
//            if(hour1<v){
//                Log.e("ATG",hour.getStart_time()+" shijia1233n");
//                return hour;
//            }
//        }
//        return new VenueHour("0","0","25:00","26:00","0");
//    }
    public VenueHour getTime(){
        BaseApplication.getDaoInstant().clear();
        List<VenueHour> venueHours = CardDao.queryHourS("1",ClassCalendar.getWeekDays()+"");
        String strhour = ClassCalendar.getHour();
        double hour1 = Double.parseDouble(strhour);
        for(VenueHour hour:venueHours){
//            Log.e("ATG",hour.getStart_time()+"    "+hour.getEnd_time());
            String start_time = hour.getStart_time();
            String start_time1 = start_time.substring(0, 2);
            String start_time2 = start_time.substring(3,4);
            String start_time3 = start_time.substring(4);
//            if (start_time3.equals("0")){
//                start_time3="01";
//            }
            String end_time = hour.getEnd_time();
            String end_time1 = end_time.substring(0, 2);
            String end_time2 = end_time.substring(3,4);
            String end_time3 = end_time.substring(4);
//            if (end_time3.equals("0")){
//                end_time3="01";
//            }
            String s = start_time1 + "." + start_time2+start_time3;
            String s1 = end_time1 + "." + end_time2+end_time3;
            double v = Double.parseDouble(s);
            double v1 = Double.parseDouble(s1);

            if (v<=hour1&&hour1<=v1){
                Log.e("ATG",hour1+" shijian"+"  "+" "+v+"  "+v1);
                return hour;
            }
        }
        for(VenueHour hour:venueHours){
            String start_time = hour.getStart_time();
            String start_time1 = start_time.substring(0, 2);
            String start_time2 = start_time.substring(3,4);
            String start_time3 = start_time.substring(4);
            if (start_time3.equals("0")){
                start_time3="1";
            }
            String s = start_time1 + "." + start_time2+start_time3;
            double v = Double.parseDouble(s);
            if(hour1<v){
                Log.e("ATG",hour.getStart_time()+" shijia1233n");
                return hour;
            }
        }
//        String id, String weekday, String start_time, String end_time,
//                String status
        return new VenueHour("0","0","0","0","0");
    }
    //登录模块
    public void Login(String name,String pwd){
        String serialNum = android.os.Build.SERIAL;
        Toast.makeText(context,serialNum,Toast.LENGTH_LONG).show();
        Log.e("ATG","设备"+serialNum);
        OkhttPost.Okget(API.LOGIN+"?account_no="+name+"&password="+pwd+"&sno="+ Constant.SNO+"&app_no="+serialNum,1,new MainCall());

    }
    public  String class_no="";
    class MainCall implements OkhttPost.Callback {
        @Override
        public void onError(Call call, Exception e, int id) {

        }

        @Override
        public void onResponse(String response, int id) {
            try{
                if (id==1){
                    Gson gson=new Gson();
                    LoginBean bean = gson.fromJson(response, LoginBean.class);
                    if (bean.code==0){
//                        Loginbroadcast(Constant.LOGIN,bean.msg,"","1");
                        Intent intent2=new Intent();
                        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent2.setAction(Constant.LOGIN);
                        intent2.putExtra("type","1");
                        intent2.putExtra(Constant.LOGIN,bean.msg);
                        context.sendBroadcast(intent2);
                    }else if (bean.code==1){
                        PrefUtild.INSTANCE.setString(context,"class_no","");
                        CardDao.deleteAll();
//                        errbroadcast("登录成功");
                        Log.e("ATG",bean.class_no);
                        class_no=bean.class_no;
                        OkhttPost.Okget(API.VENUE+ Constant.SNO+"/"+bean.class_no,2,new MainCall());

//                        PrefUtild.INSTANCE.setString(context, Constant.NEWS,"1");
                    }
                    Log.e("ATG",bean.code+"  "+bean.msg+"  "+bean.school_no+" "+bean.class_no);
                }else if (id==2){
                    Log.e("ATG",response);
                    AddData.addCardNum("test1","3428661647",0);
                    AddData.addCardNum("test2","1907098144",0);
                    AddData.addCardNum("test3","1180779457",0);
                    AddData.addCardNum("test4","1068912667",0);
                    AddData.addCardNum("test5","1139249392",0);
                    AddData.addCardNum("test5","2157230741",0);
                    PrefUtild.INSTANCE.setString(context,"class_no",class_no);
                    Gson gson=new Gson();
                    VenueBean bean=gson.fromJson(response,VenueBean.class);
                    if (bean.code==1){
                        VenueBean.Data data = bean.data;
                        VenueBean.Settings settings = data.settings;
                        List<VenueBean.Manager> manager = settings.manager;
                        Log.e("ATG","L1");
                        for (int i=0;i<manager.size();i++){
//                            String id, String title, String remark, int type
                            VenueEvaluate evaluate=new VenueEvaluate(manager.get(i).id,manager.get(i).title,manager.get(i).remark,manager.get(i).star1,manager.get(i).star2,
                                    manager.get(i).star3,manager.get(i).star4,manager.get(i).star5,"2",0);
                            CardDao.insertEvaluate(evaluate);
                        }
                        Log.e("ATG","L2");
                        List<VenueBean.Applyer> applyer = settings.applyer;
                        for (int i=0;i<applyer.size();i++){
//                            String id, String title, String remark, int type
                            VenueEvaluate evaluate=new VenueEvaluate(applyer.get(i).id,applyer.get(i).title,applyer.get(i).remark,applyer.get(i).star1,applyer.get(i).star2,
                                    applyer.get(i).star3,applyer.get(i).star4,applyer.get(i).star5,"2",1);
                            CardDao.insertEvaluate(evaluate);
                        }
                        Log.e("ATG","L3");
                        List<VenueBean.Complain> complain = settings.complain;
                        for (int i=0;i<complain.size();i++){
//                            String id, String text, int type
                            VenueKeyword keyword=new VenueKeyword(complain.get(i).id,complain.get(i).text,0);//投诉
                            CardDao.insertKeyword(keyword);
                        }
                        Log.e("ATG","L4");
                        List<VenueBean.Keyword> keywords = settings.keyword;
                        for (int i=0;i<keywords.size();i++){
//                            String id, String text, int type
                            VenueKeyword keyword=new VenueKeyword(keywords.get(i).id,keywords.get(i).text,1);//预约
                            CardDao.insertKeyword(keyword);
                        }
                        VenueBean.Venue_info venue_info = data.venue_info;
//                        String id, String venue_name, String description,
//                        String min_num, String max_num, String score, String regulation,
//                        String add_time
                        VenueInfo info=new VenueInfo(venue_info.id,venue_info.venue_name,venue_info.description,venue_info.min_num,venue_info.max_num,venue_info.score,venue_info.regulation,venue_info.add_time,venue_info.user_score);
                        CardDao.insertInfo(info);
                        Log.e("ATG","L5");
                        List<VenueBean.Managers> managers = venue_info.managers;
                        for (int i=0;i<managers.size();i++){
                            AddData.addStudent(managers.get(i).teacher_no+"t",managers.get(i).card_no==null?managers.get(i).teacher_no:managers.get(i).card_no,managers.get(i).teacher_name,"","",2);
                        }
                        List<VenueBean.ClassHour> class_hour = venue_info.class_hour;
                        Log.e("ATG","L6");
                        for (int i=0;i<class_hour.size();i++){
//                            String id, String weekday, String start_time, String end_time,
//                                    String status
                            VenueHour hour=new VenueHour(class_hour.get(i).id,class_hour.get(i).weekday,class_hour.get(i).start_time,class_hour.get(i).end_time,class_hour.get(i).status);
                            CardDao.insertHour(hour);
                            if (class_hour.get(i).status.equals("1")){
                                List<VenueBean.Members> members = venue_info.class_hour.get(i).members;
                                for (int j=0;j<members.size();j++){
//                                    String id, String booking_id, String member_id, String H_id
                                    VenueGroup group=new VenueGroup(class_hour.get(i).id+members.get(j).member_id,members.get(j).booking_id,members.get(j).member_id,class_hour.get(i).id,"0");
                                    CardDao.insertGroup(group);
                                }
                            }
                        }
                        Log.e("ATG","运行");
                        Intent intent=new Intent(context, LocalService.class);
                        context.startService(intent);
                        Intent intent1=new Intent(context, RemoteService.class);
                        context.startService(intent1);
                        Intent intent2=new Intent();
                        intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        intent2.setAction(Constant.LOGIN);
                        intent2.putExtra("type","2");
                        context.sendBroadcast(intent2);
                    }
//                    Intent intent=new Intent(context, LocalService.class);
//                    context.startService(intent);
//                    Intent intent1=new Intent(context, RemoteService.class);
//                    context.startService(intent1);
////                    Loginbroadcast(Constant.LOGIN,"","","2");
////                    intent.putExtra("type","2");
////                    intent.setAction(Constant.LOGIN);
//////                    intent.setAction(Constant.SCHEDULE);
////                    context.sendBroadcast(intent);
//                    Intent intent2=new Intent();
//                    intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    intent2.setAction(Constant.LOGIN);
//                    intent2.putExtra("type","2");
//                    context.sendBroadcast(intent2);
//                    intents.setAction(Constant.LOGIN1);
//                    intents.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    context.sendBroadcast(intents);
                    Log.e("ATG","denglu");
                }
            }catch (Exception e){}
        }
    }
}
